package com.diandi.messages.service.impl.email;

import com.alibaba.fastjson.JSONObject;
import com.diandi.messages.enums.SendStatusEnum;
import com.diandi.messages.manager.email.MessageEmailManager;
import com.diandi.messages.properties.MessageEmailProperties;
import com.diandi.messages.properties.MessagePlatformProperties;
import com.diandi.messages.service.email.MessageEmailService;
import com.diandi.messages.tools.CreateTradeNo;
import com.diandi.messages.tools.Tools;
import com.diandi.messages.util.EmailUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by mxp on 2017/9/22.
 */
@Service
public class MessageEmailServiceImpl implements MessageEmailService {

    private Logger logger = LoggerFactory.getLogger(MessageEmailServiceImpl.class);

    @Autowired
    private MessageEmailProperties emailProperties;

    @Autowired
    private MessageEmailManager emailManager;

    @Autowired
    private MessagePlatformProperties platformProperties;

    @Override
    public Map<String, String> sendEmail(HttpServletRequest request) {
        Map<String, String> returnMap = new HashMap<String, String>();
        Map<String, String> paramMap = Tools.checkBusinessSign(request, platformProperties, "platfrom");
//        Map<String, String> paramMap = Tools.getParameterMap(request, false);
        paramMap.put("apiUser", emailProperties.getEmailApiUser());
        paramMap.put("apiKey", emailProperties.getEmailApiKey());
        paramMap.put("fromAddress", emailProperties.getEmailFromAddress());
        paramMap.put("emailUrl", emailProperties.getEmailUrl());
        paramMap.put("tradeNo", CreateTradeNo.createTradeNo("TR"));
        //保存数据
        emailManager.insertEmail(paramMap);

        //发送邮件
        Map sendResult = EmailUtils.sendEmailBySohu(paramMap);

        Map<String, Object> updateParam = new HashMap<String, Object>();

        if ("success".equals(sendResult.get("message"))){
            //处理成功
            returnMap.put("code", "1000");
            returnMap.put("msg", "success");
            //更新数据库状态
            updateParam.put("status", SendStatusEnum.成功.getStatus());
        } else {
            //处理失败
            returnMap.put("code", "1001");
            returnMap.put("msg", "error");
            returnMap.put("desc", sendResult.get("errors").toString());
            //更新数据库
            updateParam.put("status", SendStatusEnum.处理失败.getStatus());
        }

        updateParam.put("returnMsg", JSONObject.toJSONString(sendResult));
        updateParam.put("messageTrade", paramMap.get("tradeNo"));
        updateParam.put("businessId", paramMap.get("businessId"));

        //更新数据状态
        Integer updateResult = emailManager.updateEmail(updateParam);

        if (updateResult == null || updateResult == 0){
            logger.warn("update email message failed , parameter is : [{}]", updateParam.toString());
        }

        return returnMap;
    }
}